package com.example.sample.util;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Build;
import android.view.View;
import android.view.ViewConfiguration;

import java.lang.reflect.Method;

/**
 * Created by LiangYu on 2018/3/16.
 */
public class VKUtil {

    /**
     * 获取导航条高度
     */
    public static int getNavigationBarHeight(Context context) {
        int result = getNavigationBarHeightByDimen(context);
        if (result == 0){
            result = getNavigationBarHeightByFind(context);
        }
        return result;
    }

    /**
     * 从dimen中获取导航条高度
     * @param context
     * @return
     */
    public static int getNavigationBarHeightByDimen(Context context) {
        int result = 0;
        if (hasNavBar(context)) {
            Resources res = context.getResources();
            int resourceId = res.getIdentifier("navigation_bar_height", "dimen", "android");
            if (resourceId > 0) {
                result = res.getDimensionPixelSize(resourceId);
            }
        }
        return result;
    }

    /**
     * 直接在DecorView父视图查找导航条。
     * 解决魅族16没有把导航条高度写入dimen的问题
     */
    private static int getNavigationBarHeightByFind(Context context){
        int result = 0;
        if (context != null && context instanceof Activity){
            Activity activity = (Activity)context;
            try {
                View root = activity.getWindow().getDecorView().getRootView();
                int resId = context.getResources().getIdentifier("navigationBarBackground", "id", "android");
                // PLAN B
                // int resId = (int)Class.forName("com.android.internal.R$id").getField("navigationBarBackground").get(null);
                View navigationBar = root.findViewById(resId);
                result = navigationBar.getHeight();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    /**
     * 检查是否存在虚拟按键栏
     *
     * @param context
     * @return
     */
    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public static boolean hasNavBar(Context context) {
        Resources res = context.getResources();
        int resourceId = res.getIdentifier("config_showNavigationBar", "bool", "android");
        if (resourceId != 0) {
            boolean hasNav = res.getBoolean(resourceId);
            // check override flag
            String sNavBarOverride = getNavBarOverride();
            if ("1".equals(sNavBarOverride)) {
                hasNav = false;
            } else if ("0".equals(sNavBarOverride)) {
                hasNav = true;
            }
            return hasNav;
        } else { // fallback
            return !ViewConfiguration.get(context).hasPermanentMenuKey();
        }
    }

    /**
     * 判断虚拟按键栏是否重写
     *
     * @return
     */
    private static String getNavBarOverride() {
        String sNavBarOverride = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            try {
                Class c = Class.forName("android.os.SystemProperties");
                Method m = c.getDeclaredMethod("get", String.class);
                m.setAccessible(true);
                sNavBarOverride = (String) m.invoke(null, "qemu.hw.mainkeys");
            } catch (Throwable e) {
            }
        }
        return sNavBarOverride;
    }
}
